import * as types from '../mutation-types.js'
import apiurl from '@/api/apiurl'
// import Api from '@/api/index'

//全局状态
const state = {
  username: '',

}

//getters
const getters = {
  username: state => {
    return state.username
  },
}

const mutations = {
  [types.SET_USERNAME] (state, username) {
    state.username = username
  },
}

const actions = {
  /**
   * 获取子账号网盘列表（和未使用的网盘）
   * @param params{ userId: 主键id }
   * @returns { status: 1, result: { gpsGpsUDishList: [{  }] }, message: '請求成功' }
   */
  async fetchListUserDish({ rootGetters }, params = {}) {
    // console.log(params,'获取子账号网盘列表参数')
    // console.log(apiurl.listUserDish)
    let groupId = rootGetters['common/groupId']
    let res = await Api.request(apiurl.listUserDish, {
      groupId: groupId,
      ...params
    })
    let resultList = []
    if(res.result.gpsGpsUDishList.length) {
      resultList = groupId ? res.result.gpsGpsUDishList.filter(item => item.groupId == groupId) : res.result.gpsGpsUDishList
    }
    return resultList
  },

  /**
   * 移除网盘
   * @param params{ dishId: 网盘id }
   * @returns { status: 1, result: {}, message: '移除成功' }
   */
  async fetchDelDish(_, params = {}) {
    // console.log(params,'移除网盘参数')
    // console.log(apiurl.delDish)
    let res = await Api.request(apiurl.delDish, {
      ...params
    })

    return res
  },

  /**
   * 添加网盘
   * @param params{ name: 名称, dishLocation: 网盘位置 }
   * @returns { status: 1, result: {}, message: '添加成功' }
   */
  async fetchAddDish(_, params = {}) {
    // console.log(params,'添加网盘参数')
    // console.log(apiurl.addDish)
    let res = await Api.request(apiurl.addDish, {
      ...params
    })

    return res
  },

  /**
   * 添加案件
   * @param params{ userId: 用户id, name: 案件名称, facilitys: 设备id, dishIds: 网盘id, lawPwd: 专案密码, groupId:群组id }
   * @returns { status: 1, result: {}, message: '添加成功' }
   */
  async fetchAddLawCase({ rootGetters }, params = {}) {
    // console.log(params,'添加案件参数')
    // console.log(apiurl.addLawCase)
    let groupId = rootGetters['common/groupId']
    let res = await Api.request(apiurl.addLawCase, {
      groupId: groupId,
      ...params
    })
    return res
  },

  /**
   * 获取案件列表
   * @param params{ userId: 用户id }
   * @returns { status: 1, result: { gpsLawCaseList: [{}] }, message: '添加成功' }
   */
  async fetchLawCaseList(_, params = {}) {
    // console.log(params,'获取案件列表参数')
    // console.log(apiurl.lawCaseList)
    let res = await Api.request(apiurl.lawCaseList, {
      ...params
    })
    // res.result.gpsLawCaseList = [{
    //   createTime: 3213, id: 111, name: '硬盘1', itemNo: '000000001', state: 0, userId: 1232
    // },{
    //   createTime: 32132, id: 1112, name: '硬盘2', itemNo: '000000002', state: 1, userId: 12321
    // },{
    //   createTime: 321323, id: 11123, name: '硬盘3', itemNo: '000000003', state: 1, userId: 12323
    // }]
    return res.result.gpsLawCaseList.length ? res.result.gpsLawCaseList :[]
  },

  /**
   * 结束案件
   * @param params{ itemNo: 案件编码 }
   * @returns { status: 1, result: {}, message: '操作成功' }
   */
  async fetchEndLawCase(_, params = {}) {
    // console.log(params,'结束案件参数')
    // console.log(apiurl.lawCaseList)
    let res = await Api.request(apiurl.endLawCase, {
      ...params
    })

    return res
  },

  /**
   * 获取案件详情
   * @param params{ itemNo: 案件编码 }
   * @returns { status: 1, result: { gpsFacilityList: [{}], gpsUDishList: [{}], gpsLawCaseInfo: {} }, message: '操作成功' }
   */
  async fetchLawCaseInfo({ rootGetters }, params = {}) {
    let mapParams = rootGetters['map/mapParams']
    let res = await Api.request(apiurl.lawCaseInfo, {
      itemNo: mapParams,
      ...params
    })
    return res
  },

  /**
   * 修改案件详情
   * @param params{ lawCaseId: 案件编码, userId: '帳號id', dishIds: 網盤id, facilitys: 設備id, name: '', lawPWd: 密碼, groupId: '群組id'}
   * @returns { status: 1, result: { gpsFacilityList: [{}], gpsUDishList: [{}], gpsLawCaseInfo: {} }, message: '操作成功' }
   */
  async fetchLawEditLawCase({ rootGetters }, params = {}) {
    let groupId = rootGetters['common/groupId']
    let res = await Api.request(apiurl.editLawCase, {
      groupId: groupId,
      ...params
    })
    return res
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  getters,
  actions
}
